package com.yh.mybatis.extention.plugin.mapper;

import com.yh.mybatis.extention.plugin.comment.YhCommentGenerator;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author liubing
 */
public class YhJavaMapperMethodGenerator extends AbstractJavaMapperMethodGenerator {

    @Override
    public void addInterfaceElements(Interface interfaze) {
        // 先创建import对象
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        // 设置参数类型是对象
        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        // import参数类型对象
        importedTypes.add(parameterType);
        interfaze.addImportedTypes(importedTypes);

        interfaze.addImportedType(FullyQualifiedJavaTypeProxy.getBaseMapperInstance());
        //实体类
        FullyQualifiedJavaType myBatisRepositoryInterfaceType = new FullyQualifiedJavaType(
                new StringBuilder("IMyBatisRepository<")
                        .append(introspectedTable.getBaseRecordType())
                        .append(">")
                        .toString()
        );
        interfaze.addSuperInterface(myBatisRepositoryInterfaceType);
        //分页查询方法
        FullyQualifiedJavaType paginationDaoInterfaceType = new FullyQualifiedJavaType(
                new StringBuilder("IPaginationDao<")
                        .append(introspectedTable.getBaseRecordType())
                        .append(">")
                        .toString()
        );
        interfaze.addImportedType(FullyQualifiedJavaTypeProxy.getBasePageInstance());
        interfaze.addSuperInterface(paginationDaoInterfaceType);
        //java 注释代码
        addJavaDocLine(interfaze);
    }

    /**
     * 注释
     * @param interfaze
     */
    private void addJavaDocLine(Interface interfaze){
        interfaze.addJavaDocLine("/**");
        interfaze.addJavaDocLine(" * " + interfaze.getType().getFullyQualifiedName() + "<p/>");
        interfaze.addJavaDocLine(" * @date " + new Date().toString());
        interfaze.addJavaDocLine(" * @author jingshao" );
        interfaze.addJavaDocLine(" *");
        interfaze.addJavaDocLine(" */");
    }
}
